package com.cloud.shopping.seckill.mq;

import com.alibaba.fastjson.JSON;
import com.cloud.shopping.seckill.service.SeckillOrderService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author John.zhang
 * @version Id: OrderQueueListener, v 1.0 2021/2/8 11:07 ZhiYong Exp $
 */

@RocketMQMessageListener(
        topic = "order-queue",
        consumerGroup = "orderQueue-consumer",
        selectorExpression = "*"
)
@Component
public class OrderQueueListener implements RocketMQListener {

    @Resource
    private SeckillOrderService seckillOrderService;

    @Override
    public void onMessage(Object message) {
        int count = seckillOrderService.add(JSON.parseObject(message.toString(), Map.class));
        //根据count判断抢单是否成功
        //如果成功了，则将数据推送到消息平台->和客户端进行通信的消息平台->Netty+Websocket
    }
}
